package com.bigpig245.mangareader.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigpig245.mangareader.R;
import com.bigpig245.mangareader.entity.AbsImageLink;
import com.bigpig245.mangareader.utils.ImageUtils;

public class SimpleArrayAdapter extends ArrayAdapter<AbsImageLink> {
	private final Context context;
	private final List<AbsImageLink> values;

	public SimpleArrayAdapter(Context context, List<AbsImageLink> values) {
		super(context, R.layout.row_layout, values);
		this.context = context;
		this.values = values;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView = inflater.inflate(R.layout.row_layout, parent, false);
		TextView textView = (TextView) rowView.findViewById(R.id.label);
		ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);

		String title = values.get(position).getTitle();
		textView.setText(title);
		// Change the icon for Windows and iPhone
		String url = values.get(position).getUrl();

		Drawable d = ImageUtils.getImageFromURL(url, title);
		imageView.setImageDrawable(d);

		return rowView;
	}
}
